#ifndef LOGWRAPPER_H
#define LOGWRAPPER_H

#include "Log.h"
#include <sstream>
using namespace std;

//namespace SERVERTOOL
//{
//    extern string dump(const char* descrip, const char* data, int nbuflen);
//}

using SERVERTOOL::Log;

/* The macro without "_" is similar to the C-style printf(format, ...) */

/*LOGFATAL(1, "fatal error %d", iret*/
#define LOGFATAL(errorCode,...) \
    do \
        { \
            if(Log::instance()->doNothing(SERVERFATAL)) break; \
            Log::instance()->lock(); \
            Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
            Log::instance()->logRoutFormat(SERVERFATAL, errorCode, __VA_ARGS__); \
            Log::instance()->unlock(); \
        }while(0)

/*LOGERR(1, "error %d", iret)*/
#define LOGERR(errorCode,...) \
    do \
        { \
            if(Log::instance()->doNothing(SERVERERROR)) break; \
            Log::instance()->lock(); \
            Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
            Log::instance()->logRoutFormat(SERVERERROR, errorCode, __VA_ARGS__); \
            Log::instance()->unlock(); \
        }while(0)

/*LOGWARN("warn %d", iret)*/
#define LOGWARN(...)\
    do \
        { \
            if(Log::instance()->doNothing(SERVERWARN)) break; \
            Log::instance()->lock(); \
            Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
            Log::instance()->logRoutFormat(SERVERWARN, 0, __VA_ARGS__); \
            Log::instance()->unlock(); \
        }while(0)

/*LOGINFO("info %d", iret) */
#define LOGINFO(...) \
    do \
        { \
            if(Log::instance()->doNothing(SERVERINFO)) break; \
            Log::instance()->lock();\
            Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
            Log::instance()->logRoutFormat(SERVERINFO, 0,  __VA_ARGS__);\
            Log::instance()->unlock();\
        }while(0)

/*LOGDEBUG("debug %d", iret)*/
#define LOGDEBUG(...) \
    do \
        { \
            if(Log::instance()->doNothing(SERVERDEBUG)) break; \
            Log::instance()->lock(); \
            Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
            Log::instance()->logRoutFormat(SERVERDEBUG, 0, __VA_ARGS__); \
            Log::instance()->unlock(); \
        }while(0)

/*LOGTRACE("trace %d", iret)*/
#define LOGTRACE(...) \
    do \
        { \
            if(Log::instance()->doNothing(SERVERTRACE)) break; \
            Log::instance()->lock(); \
            Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
            Log::instance()->logRoutFormat(SERVERTRACE, 0, __VA_ARGS__); \
            Log::instance()->unlock(); \
        }while(0)

/*LOGSTAT("stat %d", iret)*/
#define LOGSTAT(...) \
    do \
        { \
            if(Log::instance()->doNothing(SERVERSTAT)) break; \
            Log::instance()->lock(); \
            Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
            Log::instance()->logRoutFormat(SERVERSTAT, 0, __VA_ARGS__); \
            Log::instance()->unlock(); \
        }while(0)

/*LOGDUMP("descript", "data", iLen)*/
#define LOGDUMP(descrip, data, len) \
    do \
        { \
            if(Log::instance()->doNothing(SERVERDUMP)) break; \
            Log::instance()->lock(); \
            Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
            Log::instance()->logRoutStream(SERVERDUMP, 0, SERVERTOOL::dump(descrip, data, len)); \
            Log::instance()->unlock(); \
        }while(0)

/*The macro with "_" is similar to the C++-style cout << logEvent << others... */
#define LOG_FATAL(errorCode, logEvent) \
               do \
               { \
                   if( Log::instance()->doNothing(SERVERFATAL) )  break ; \
                   ostringstream ostr; \
                   ostr<<logEvent; \
                   Log::instance()->lock(); \
                   Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
                   string strLogData = ostr.str(); \
                   Log::instance()->logRoutStream(SERVERFATAL, errorCode, strLogData); \
                   Log::instance()->unlock(); \
               }while(0)

#define LOG_ERR(errorCode, logEvent) \
               do \
               { \
                   if( Log::instance()->doNothing(SERVERERROR) )  break ; \
                   ostringstream ostr; \
                   ostr<<logEvent; \
                   Log::instance()->lock(); \
                   Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
                   string strLogData = ostr.str(); \
                   Log::instance()->logRoutStream(SERVERERROR, errorCode, strLogData); \
                   Log::instance()->unlock(); \
               }while(0)

#define LOG_WARN(logEvent) \
               do \
               { \
                   if( Log::instance()->doNothing(SERVERWARN) )  break ; \
                   ostringstream ostr; \
                   ostr<<logEvent; \
                   Log::instance()->lock(); \
                   Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
                   string strLogData = ostr.str(); \
                   Log::instance()->logRoutStream(SERVERWARN, 0, strLogData); \
                   Log::instance()->unlock(); \
               }while(0)

#define LOG_INFO(logEvent) \
               do \
               { \
                   if( Log::instance()->doNothing(SERVERINFO) )  break ; \
                   ostringstream ostr; \
                   ostr<<logEvent; \
                   Log::instance()->lock(); \
                   Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
                   string strLogData = ostr.str(); \
                   Log::instance()->logRoutStream(SERVERINFO, 0, strLogData); \
                   Log::instance()->unlock(); \
               }while(0)

#define LOG_DEBUG(logEvent) \
               do \
               { \
                   if( Log::instance()->doNothing(SERVERDEBUG) )  break ; \
                   ostringstream ostr; \
                   ostr<<logEvent; \
                   Log::instance()->lock(); \
                   Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
                   string strLogData = ostr.str(); \
                   Log::instance()->logRoutStream(SERVERDEBUG, 0, strLogData); \
                   Log::instance()->unlock(); \
               }while(0)

#define LOG_TRACE(logEvent) \
               do \
               { \
                   if( Log::instance()->doNothing(SERVERTRACE) )  break ; \
                   ostringstream ostr; \
                   ostr<<logEvent; \
                   Log::instance()->lock(); \
                   Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
                   string strLogData = ostr.str(); \
                   Log::instance()->logRoutStream(SERVERTRACE, 0, strLogData); \
                   Log::instance()->unlock(); \
               }while(0)

#define LOG_STAT(logEvent) \
               do \
               { \
                   if( Log::instance()->doNothing(SERVERSTAT) )  break ; \
                   ostringstream ostr; \
                   ostr<<logEvent; \
                   Log::instance()->lock(); \
                   Log::instance()->setFileLine(basename((char *)__FILE__), __LINE__);  \
                   string strLogData = ostr.str(); \
                   Log::instance()->logRoutStream(SERVERSTAT, 0, strLogData); \
                   Log::instance()->unlock(); \
               }while(0)

#define LOG_SET_SYSID(pConstChar) Log::instance()->setSysID(pConstChar)
#define LOG_SET_PID(pConstChar) Log::instance()->setPID(pConstChar)
#define LOG_SET_CWA(pConstChar) Log::instance()->setCWA(pConstChar)
#define LOG_SET_SEQUENCEID(pConstChar) Log::instance()->setSequenceID(pConstChar)
#define LOG_SET_TRANSERIALNO(pConstChar) Log::instance()->setTranSerialNo(pConstChar)
#define LOG_SET_TRACEFLAG(ONEorZERO) Log::instance()->setTraceFlag(ONEorZERO)
#define LOG_SET_CALLERRMNGFLAG(ONEorZERO) Log::instance()->setCallErrMngFlag(ONEorZERO)
#define LOG_CLEAR_USERVALUE() Log::instance()->clearUserRelateValue()
#define LOG_SET_WARNFLAG(ONEorZERO) Log::instance()->setWarnFlag(ONEorZERO)
#define LOG_SET_INFOFLAG(ONEorZERO) Log::instance()->setInfoFlag(ONEorZERO)
#define LOG_SET_DUMPFLAG(ONEorZERO) Log::instance()->setDumpFlag(ONEorZERO)
#define LOG_SET_DEBUGFLAG(ONEorZERO) Log::instance()->setDebugFlag(ONEorZERO)

#endif
